/*
 *      Author: Honrun
 */
#include "CSU8RP3216.h"
#include "stdint.h"
#include "DevicesTime.h"
#include "DevicesDelay.h"


/*
 * Return:      void
 * Parameters:  Time: 延时时间（范围：1~59，系统时钟为72M时）
 * Description: 秒延时
 */
void vDelayS(float fTime)
{
    int32_t iTime = fTime;

    while((iTime--) > 0)
    {
        vDelayUs(fTime * 1000000.0f);
    }

    fTime -= iTime;
    vDelayUs(fTime * 1000000.0f);
}

/*
 * Return:      void
 * Parameters:  Time: 延时时间（范围：1~59652，系统时钟为72M时）
 * Description: 毫秒延时
 */
void vDelayMs(float fTime)
{
    vDelayUs(fTime * 1000.0f);
}

/*
 * Return:      void
 * Parameters:  Time: 延时时间（范围：1~59652323，系统时钟为72M时）
 * Description: 微秒延时
 */
void vDelayUs(float fTime)
{
    volatile uint32_t uiTimeStop = 0u, uiTimeStart = 0;

    if(fTime < 0.01f)
        return;

    uiTimeStart = lTimeGetStamp();
    uiTimeStop  = (uint32_t)(fTime) + uiTimeStart;

    if(uiTimeStop >= uiTimeStart)
        while((lTimeGetStamp() > uiTimeStart) && (lTimeGetStamp() < uiTimeStop));
    else
        while(!((lTimeGetStamp() > uiTimeStop) && (lTimeGetStamp() < uiTimeStart)));
}
